﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework
 * Version 2.10
 * 
 * POP3 PASS Command Class
 *      Represents the password (PASS) command.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using Adaptive.Foundation;
using Adaptive.Foundation.Network.Cnr;
using Adaptive.Network.Sockets;

namespace Adaptive.Foundation.Network.Pop3
{
    /// <summary>
    /// Represents the password (PASS) command.
    /// </summary>
    public class PassCommand : CnrSocketsCommandBase
    {
        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public PassCommand(SocketsClient client)
            : base(client, "PASS", (int)Pop3Commands.Pass)
        {
            Parameters.Add(new CnrCommandParameter<string>("password", ""));
        }
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="client">
        /// </param>
        /// <param name="password">
        /// The password value to use.
        /// </param>
        public PassCommand(SocketsClient client, string password)
            : base(client, "PASS", (int)Pop3Commands.Pass)
        {
            Parameters.Add(new CnrCommandParameter<string>("password", password));
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        /// <summary>
        /// Executes the command.
        /// </summary>
        /// <returns>
        /// A <see cref="CnrResponseBase"/> instance containing the response to the command execution.
        /// </returns>
        public override CnrResponseBase Execute()
        {
            Pop3Response response = null;
            string data = RenderCommand() + "\r\n";
            string result = string.Empty;

            result = Client.SendAndReceive(data, "\r\n", 30);
            if ((result != null) && (result.Length > 0))
            {
                response = new Pop3Response(result);
            }
            else
            {
                response = new Pop3Response();
            }

            return response;
        }
        /// <summary>
        /// Executes the command using the specified object.
        /// </summary>
        /// <param name="executionContextItem">
        /// An object containing data and/or used to execute the command.
        /// </param>
        /// <returns>
        /// A <see cref="CnrResponseBase"/> instance containing the response to the command execution.
        /// </returns>
        public override CnrResponseBase Execute(object executionContextItem)
        {
            return Execute();
        }
        /// <summary>
        /// Renders the command as a string.
        /// </summary>
        /// <returns>
        /// A string containing the command and parameter values.
        /// </returns>
        protected override string RenderCommand()
        {
            string returnValue = string.Empty;

            returnValue = "PASS";
            if (Parameters["password"] != null)
                returnValue += " " + ((CnrCommandParameter<string>)Parameters["password"]).DataContent;

            return returnValue;
        }
        /// <summary>
        /// Renders the command as a string.
        /// </summary>
        /// <returns>
        /// A string containing the command and parameter values.
        /// </returns>
        /// <param name="renderer">
        /// A <see cref="CnrDataRendererBase"/> instance used to render the command.
        /// </param>
        protected override string RenderCommand(CnrDataRendererBase renderer)
        {
            string returnValue = string.Empty;

            returnValue = ((Pop3Renderer)renderer).RenderPop3Command(Pop3Commands.User);
            if (Parameters["password"] != null)
                returnValue += " " + ((CnrCommandParameter<string>)Parameters["password"]).DataContent;

            return returnValue;
        }
        /// <summary>
        /// Parses the original data content into the command object.
        /// </summary>
        /// <param name="parser">
        /// A <see cref="CnrDataParserBase"/> instance used to parse the command text.
        /// </param>
        /// <param name="originalData">
        /// The original data to be parsed.
        /// </param>
        protected override void ParseCommand(CnrDataParserBase parser, string originalData)
        {
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Clears the contents of the object instance without disposing of the instance.
        /// </summary>
        public override void Clear()
        {
            if (Parameters["password"] != null)
                ((CnrCommandParameter<string>)Parameters["password"]).DataContent = string.Empty;
        }
        #endregion
    }
}